﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsView;
using WindowsFormsPresenter;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter.Restaurant;

namespace RestaurantManagement
{
    public partial class frmLogin : Form, ILoginView, IRestaurantsView
    {
        private LoginPresenter _loginPresenter;
        private RestaurantsPresenter _restaurantsPresenter;

        public IList<RestaurantModel> Restaurants
        {
            set { var restaurants = value; AddRestaurantToCombobox(restaurants); }
        }

        public string UserName
        {
            get { return txtUserName.Text.ToString(); }
        }

        public string Password
        {
            get { return txtPassword.Text.ToString(); }
        }

        public string RestaurantId
        {
            get { return cboRestaurantId.EditValue.ToString(); }
        }

        private void AddRestaurantToCombobox(IList<RestaurantModel> restaurants)
        {
            cboRestaurantId.Properties.DataSource = restaurants;
            cboRestaurantId.Properties.DisplayMember = "RestaurantName";
            cboRestaurantId.Properties.ValueMember = "RestaurantID";

            cboRestaurantId.Properties.PopulateColumns();
            //for (int i = 0; i < cboRestaurantId.Properties.Columns.Count; i++) { cboRestaurantId.Properties.Columns[i].Visible = false; }

            //cboRestaurantId.Properties.Columns["RestaurantName"].Visible = true;
            //cboRestaurantId.Properties.Columns["Address"].Visible = true;
            //cboRestaurantId.Properties.Columns["PhoneNumber"].Visible = true;

            //cboRestaurantId.Properties.Columns["RestaurantName"].Caption = "Tên nhà hàng";
            //cboRestaurantId.Properties.Columns["Address"].Caption = "Địa chỉ";
            //cboRestaurantId.Properties.Columns["PhoneNumber"].Caption = "Số điện thoại";

            cboRestaurantId.ItemIndex = 1;
        }

        public frmLogin()
        {
            InitializeComponent();

            _loginPresenter = new LoginPresenter(this);
            _restaurantsPresenter = new RestaurantsPresenter(this);
            _restaurantsPresenter.Display();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                _loginPresenter.Login();

                frmMain frm = new frmMain();
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Đăng nhập thất bại");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
